import { EnumHttpMime } from "@/types/global/enums/enums_types"
import request from "@/utils/httpRequest";
// import AxiosHeaderValue from 
/**
 * 下载文件
 * @param fileData 二进制数据
 * @param fileType mime类型，如: excel 文件 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
 * @param fileName download属性保存的文件名
 */
export function downloadFile(fileData: Blob, contentType: any | undefined, fileName: string) {
    const blob = new Blob([fileData], { type: contentType })
    // const blob = new Blob([fileData])
    const downloadUrl = window.URL.createObjectURL(blob)

    const downloadLink = document.createElement('a')
    downloadLink.href = downloadUrl
    downloadLink.download = fileName

    document.body.appendChild(downloadLink)
    downloadLink.click()

    document.body.removeChild(downloadLink)
    window.URL.revokeObjectURL(downloadUrl)
}

export function downloadFileByUrlMethodQuery(url:string,method :'post'|'get' ='post',queryParams?:object){
    let promise=request({
        url,
        method,
        data:queryParams,
        responseType: 'arraybuffer',
    });
    promise.then((response ) => {
        const fileData = response.data
        const fileName = decodeURIComponent(
            response.headers['content-disposition'].split(';')[1].split('=')[1],
        )
        const contentType=response.headers["Content-Type"];
        // const fileType =
        //     'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8';
       downloadFile(fileData, contentType, fileName);
    })

}



const NslUtilDownUpLoad = {
    downloadFile,
    downloadFileByUrlMethodQuery
  
};

export {NslUtilDownUpLoad}

export default  NslUtilDownUpLoad ;